package com.aidong.web.controller.dic;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicShopLevelDao;
import com.aidong.model.wills.DicShopLevel;
import com.aidong.web.controller.BaseController;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"dicShopLevel"})
public class DicShopLevelController extends BaseController {
    @Resource
    DicShopLevelDao dicShopLevelDao;

    public DicShopLevelController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("dic/shoplevel/list")).addObject("setType", this.getTypeList("setType"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap(1);
        if(!this.chkNull(name)) {
            cd.like("code", "%" + name + "%");
            cd.like("chName", "%" + name + "%");
        }

        this.dicShopLevelDao.queryPageList(pg, cd, (new OrderSet()).asc("code"));
        ModelAndView mv = new ModelAndView("dic/shoplevel/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        DicShopLevel level = new DicShopLevel();
        ModelAndView mv = new ModelAndView("dic/shoplevel/edit");
        mv.addObject("level", level);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("level") DicShopLevel level) {
        if(this.chkNull(level.getCode())) {
            return this.getResult(false, "请输入编号");
        } else if(this.chkNull(level.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.dicShopLevelDao.exists(level.getCode())) {
            return this.getResult(false, "编号已存在");
        } else {
            level.setCreateUser(this.getLoginUserCode());
            level.setCreateTime(new Date());
            level.setUpdateTime(new Date());

            try {
                this.dicShopLevelDao.add(level);
                this.reloadTypeList("shopLevel");
                this.addUserLog("新增门店级别", "编号:" + level.getCode() + ",名称:" + level.getChName());
            } catch (Exception var3) {
                this.log.error("add shoplevel", var3);
                return this.getResult(false, "保存发生错误");
            }

            return this.getResult(true, "保存完成");
        }
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("code") String code) {
        DicShopLevel level = (DicShopLevel)this.dicShopLevelDao.getById(code);
        if(level == null) {
            return this.showAlert("没有这个记录");
        } else {
            ModelAndView mv = new ModelAndView("dic/shoplevel/edit");
            mv.addObject("level", level);
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("level") DicShopLevel level) {
        if(this.chkNull(level.getCode())) {
            return this.getResult(false, "请输入编号");
        } else if(this.chkNull(level.getChName())) {
            return this.getResult(false, "请输入名称");
        } else {
            DicShopLevel level1 = (DicShopLevel)this.dicShopLevelDao.getById(level.getCode());
            if(level1 == null) {
                return this.getResult(false, "没有这个记录");
            } else {
                level1.setChName(level.getChName());
                level1.setUpdateTime(new Date());

                try {
                    this.dicShopLevelDao.update(level1);
                    this.reloadTypeList("shopLevel");
                    this.addUserLog("修改门店级别", "编号:" + level.getCode() + ",名称:" + level.getChName());
                } catch (Exception var4) {
                    this.log.error("edit shoplevel", var4);
                    return this.getResult(false, "保存发生错误");
                }

                return this.getResult(true, "保存完成");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("code") String code) {
        DicShopLevel level = (DicShopLevel)this.dicShopLevelDao.getById(code);
        if(level == null) {
            return this.getResult(false, "没有这个记录");
        } else {
            try {
                this.dicShopLevelDao.delete(level);
                this.reloadTypeList("shopLevel");
                this.addUserLog("删除门店级别", "编号:" + level.getCode() + ",名称:" + level.getChName());
            } catch (Exception var4) {
                this.log.error("del shoplevel", var4);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}